package com.iqiju.guagua.redtone.video.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 接口返回通用封装
 */
@Data
@ApiModel("通用接口返回对象")
public class Results<T> {

    /** 成功code */
    public static final int SUCCESS_CODE = 0;
    /** 参数错误 state */
    private static final int PARAM_ERROR_CODE = 100;
    /** 未知错误 state */
    private static final int UNKNOWN_ERROR_CODE = 500;
    /** 登录过期或者未登录 state */
    private static final int LOGIN_EXPIRE_ERROR_CODE = 401;
    /** token错误 state */
    private static final int TOKEN_ERROR_CODE = 405;


    /** 成功 message */
    public static final String SUCCESS_MSG = "SUCCESS";
    /** 位置错误 message */
    private static final String UNKNOWN_ERROR = "UNKNOWN_ERROR";
    /** 登录过期或者未登录 */
    private static final String LOGIN_EXPIRE_ERROR_MSG = "登录过期或者未登录";
    /** token错误 */
    private static final String TOKEN_ERROR_MSG = "token错误";



    @ApiModelProperty(required = true,notes = "结果码",example = "0")
    private int state;
    @ApiModelProperty(required = true,notes = "时间戳",example = "1567425139000")
    private long time;
    @ApiModelProperty(required = true,notes = "返回信息",example = "SUCCESS")
    private String message;
    @ApiModelProperty(required = true,notes = "返回数据")
    private T content;

    private Results() {}

    /** 默认成功结果 */
    public static Results success(){
        Results results = new Results();
        results.setState(SUCCESS_CODE);
        results.setMessage(SUCCESS_MSG);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /** 默认成功结果 */
    public static Results success(Object content){
        Results results = new Results();
        results.setState(SUCCESS_CODE);
        results.setMessage(SUCCESS_MSG);
        results.setContent(content);
        results.setTime(System.currentTimeMillis());
        return results;
    }

    /** 默认未知错误结果 */
    public static Results error() {
        Results results = new Results();
        results.setState(UNKNOWN_ERROR_CODE);
        results.setMessage(UNKNOWN_ERROR);
        results.time = System.currentTimeMillis();
        return results;
    }

    /** 登录过期或者未登录 */
    public static Results loginExpireError() {
        Results results = new Results();
        results.setState(LOGIN_EXPIRE_ERROR_CODE);
        results.setMessage(LOGIN_EXPIRE_ERROR_MSG);
        results.time = System.currentTimeMillis();
        return results;
    }

    /** token错误 */
    public static Results tokenError() {
        Results results = new Results();
        results.setState(TOKEN_ERROR_CODE);
        results.setMessage(TOKEN_ERROR_MSG);
        results.time = System.currentTimeMillis();
        return results;
    }

    /** 未知错误结果  自定义消息 */
    public static Results error(String message) {
        Results results = new Results();
        results.setState(UNKNOWN_ERROR_CODE);
        results.setMessage(message);
        results.time = System.currentTimeMillis();
        return results;
    }


    /** 参数错误结果 */
    public static Results paramError(String msg) {
        Results results = new Results();
        results.setState(PARAM_ERROR_CODE);
        results.setMessage(msg);
        results.time = System.currentTimeMillis();
        return results;
    }
}
